package org.jawa.model;

import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;

import java.io.Serializable;

@Entity
@Table(name = "category")
public class Category extends BaseObject implements Serializable {

	private static final long serialVersionUID = 4347702985049653852L;
	private Long id;
	private String catename;
	private String catedesc;
	private Set<Product> products = new HashSet<Product>();

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Long getId() {
		return this.id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Column(name = "CATENAME", unique = true, length = 50)
	public String getCatename() {
		return this.catename;
	}

	public void setCatename(String catename) {
		this.catename = catename;
	}

	@Column(name = "CATEDESC", length = 200)
	public String getCatedesc() {
		return this.catedesc;
	}

	public void setCatedesc(String catedesc) {
		this.catedesc = catedesc;
	}

	// @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy ="category")
	@OneToMany(targetEntity = Product.class, cascade = { CascadeType.PERSIST,
			CascadeType.MERGE }, mappedBy = "category", fetch = FetchType.EAGER)
	@OrderBy
	public Set<Product> getProducts() {
		return this.products;
	}

	public void setProducts(Set<Product> products) {
		this.products = products;
	}

	public boolean equals(Object o) {
		if (this == o)
			return true;
		if (o == null || getClass() != o.getClass())
			return false;

		Category pojo = (Category) o;

		if (catename != null ? !catename.equals(pojo.catename)
				: pojo.catename != null)
			return false;
		if (catedesc != null ? !catedesc.equals(pojo.catedesc)
				: pojo.catedesc != null)
			return false;

		return true;
	}

	public int hashCode() {
		int result = 0;
		result = (catename != null ? catename.hashCode() : 0);
		result = 31 * result + (catedesc != null ? catedesc.hashCode() : 0);

		return result;
	}

	public String toString() {
		StringBuffer sb = new StringBuffer(getClass().getSimpleName());

		sb.append(" [");
		sb.append("id").append("='").append(getId()).append("', ");
		sb.append("catename").append("='").append(getCatename()).append("', ");
		sb.append("catedesc").append("='").append(getCatedesc()).append("', ");

		sb.append("]");

		return sb.toString();
	}

}
